<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Update extends CI_Controller{    	
    function __construct(){
		parent::__construct();	
        $this->load->dbforge();	
	}
    function index(){
        $xml = simplexml_load_file("http://www.geoplugin.net/xml.gp?ip=123.125.114.144");
echo $xml->geoplugin_countryCode ;
        //them bang country va them truong country vao offer
        if (!$this->db->table_exists('country')){       
            $fields = array(
                            'id' => array(
                                                     'type' => 'INT',
                                                     'constraint' => 11,
                                                     'unsigned' => TRUE,
                                                     'auto_increment' => TRUE
                                              ),
                           
                            'country' => array(
                                                     'type' => 'VARCHAR',
                                                     'constraint' => '200',
                                                     'default'=>'0'
                                              ),                            
                            'show' => array(
                                                     'type' =>'INT',
                                                     'constraint' => 2,
                                                     'default'=>'1'
                                              ),
                            'code' => array(
                                                     'type' =>'VARCHAR',
                                                     'constraint' => '10',                                                 
                                              )
                            
                    );
            $this->dbforge->add_field($fields); 
            $this->dbforge->add_key('id', TRUE);
            $this->dbforge->create_table('country',true);
        }
        //them truong vao bang offer
        if (!$this->db->field_exists('country', 'offer'))
        {
            $this->dbforge->add_column('offer', array('country' => array('type' => 'VARCHAR','constraint' => 200,'DEFAULT'=>'o')));
        }
        /*//da update xong
        //thay doi ten 
        //$this->dbforge->modify_column('offer', array('country' => array( 'type' => 'TEXT',)));
        //them truong request vao bang offer
        if (!$this->db->field_exists('request', 'offer'))
        {
            $this->dbforge->add_column('offer', array('request' => array('type' => 'VARCHAR','constraint' => 2,'DEFAULT'=>0)));
        }
        // them truong capped
        if (!$this->db->field_exists('capped', 'offer'))
        {
            $this->dbforge->add_column('offer', array('capped' => array('type' => 'INT','constraint' => 10,'DEFAULT'=>0)));
        }
        // addd them user_id vao bang report
        if (!$this->db->field_exists('id_user', 'report'))
        {
            $this->dbforge->add_column('report', array('id_user' => array('type' => 'INT','constraint' => 10,'DEFAULT'=>0)));
        }
        //////////////////////////////
       
        if (!$this->db->table_exists('request')){       
            $fields = array(
                            'id' => array(
                                                     'type' => 'INT',
                                                     'constraint' => 11,
                                                     'unsigned' => TRUE,
                                                     'auto_increment' => TRUE
                                              ),
                            'crequest' => array(
                                                     'type' => 'text'                                                 
                                              ),
                            'status' => array(
                                                     'type' => 'VARCHAR',
                                                     'constraint' => '200',
                                                     'default'=>'Pending'
                                              ),
                            'id_user' => array(
                                                     'type' =>'INT',
                                                     'constraint' => 11,
                                                     'unsigned' => TRUE,                                                 
                                              ),
                            'id_offer' => array(
                                                     'type' =>'INT',
                                                     'constraint' => 11,
                                                     'unsigned' => TRUE,
                                              ),
                            'ip' => array(
                                                     'type' =>'VARCHAR',
                                                     'constraint' => '20',                                                 
                                              ),
                            'date TIMESTAMP DEFAULT CURRENT_TIMESTAMP',
                    );
            $this->dbforge->add_field($fields); 
            $this->dbforge->add_key('id', TRUE);
            $this->dbforge->create_table('request',true);
        }
        /////////////update statu trong report theo offer
        $offer = $this->db->get('offer')->result();
        foreach ($offer as $offer){
            $this->db->where('idoffer',$offer->id);
            $this->db->update('report',array('status'=>$offer->show));
        }
        //update lai bang report
        $this->db->set('approved','Earns',false);
        $this->db->update('report');
        */
        
        /*//update cu de tham khao
        ///xoa bang offer
        if ($this->db->field_exists('idpb', 'offer'))
        {
              $this->dbforge->drop_column('offer', 'idpb');
        } 
        if ($this->db->field_exists('pb_value', 'offer'))
        {
               $this->dbforge->drop_column('offer', 'pb_value');
        } 
        if ($this->db->field_exists('pb_pass', 'offer'))
        {
               $this->dbforge->drop_column('offer', 'pb_pass');
        } 
        
        $fields1 = array(
                        'linkadd' => array('type' => 'TEXT')
        );
        $this->dbforge->add_column('network', $fields1);
        //thieu htem cot link add vao bang net
        
        */
        echo 'Update done!';
        
    }
    
    
 
 }